package com.newway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.common.context.UserInfoContext;
import com.newway.common.result.Result;
import com.newway.entity.Member;
import com.newway.entity.Reply;
import com.newway.entity.TopicForum;
import com.newway.mapper.MemberMapper;
import com.newway.mapper.ReplyMapper;
import com.newway.mapper.TopicForumMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/topic/forum")
public class TopicForumController {

    @Resource
    TopicForumMapper topicForumMapper;

    @Resource
    ReplyMapper replyMapper;

    @Resource
    MemberMapper memberMapper;

    @GetMapping("/list")
    public Result getList(TopicForum topicForum){
        Page<TopicForum> topicForumPage = new Page<>(topicForum.getPageNum(), topicForum.getPageSize());
        QueryWrapper<TopicForum> topicForumQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(topicForum.getTitle())){
            topicForumQueryWrapper.like("title",topicForum.getTitle());
        }
        if(StringUtils.isNotBlank(topicForum.getCategory())){
            topicForumQueryWrapper.eq("category",topicForum.getCategory());
        }
        topicForumQueryWrapper.orderByDesc("is_first");
        topicForumQueryWrapper.orderByDesc("create_time");
        Page<TopicForum> result = topicForumMapper.selectPage(topicForumPage, topicForumQueryWrapper);
        return Result.success(result);
    }

    @GetMapping("/listMy")
    public Result getListMy(TopicForum topicForum){
        Page<TopicForum> topicForumPage = new Page<>(topicForum.getPageNum(), topicForum.getPageSize());
        QueryWrapper<TopicForum> topicForumQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(topicForum.getTitle())){
            topicForumQueryWrapper.like("title",topicForum.getTitle());
        }
        if(StringUtils.isNotBlank(topicForum.getCategory())){
            topicForumQueryWrapper.eq("category",topicForum.getCategory());
        }
        topicForumQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        topicForumQueryWrapper.orderByDesc("is_first");
        topicForumQueryWrapper.orderByDesc("create_time");
        Page<TopicForum> result = topicForumMapper.selectPage(topicForumPage, topicForumQueryWrapper);
        return Result.success(result);
    }
    @PostMapping("/add")
    public Result add(@RequestBody TopicForum topicForum){
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        Member member = memberMapper.selectOne(memberQueryWrapper);
        if(member==null || !"1".equals(member.getStatus())){
            return Result.fail("请入团后再操作");
        }
        topicForum.setCreateTime(LocalDateTime.now());
        topicForum.setCreateUser(UserInfoContext.getUserInfo().getNickName()==null?UserInfoContext.getUserInfo().getUserName():UserInfoContext.getUserInfo().getNickName());
        topicForumMapper.insert(topicForum);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        topicForumMapper.deleteById(id);
        return Result.success();
    }

    @PostMapping("/detail/{id}")
    public Result detail(@PathVariable("id")Integer id){
        TopicForum topicForum = topicForumMapper.selectById(id);
        topicForum.setViewCount(topicForum.getViewCount()+1);
        topicForumMapper.updateById(topicForum);
        return Result.success(topicForum);
    }

    @GetMapping("/replyList/{forumId}")
    public Result getReplyList(@PathVariable("forumId")Integer forumId){
        QueryWrapper<Reply> replyQueryWrapper = new QueryWrapper<>();
        replyQueryWrapper.eq("forum_id",forumId);
        replyQueryWrapper.orderByDesc("id");
        List<Reply> replies = replyMapper.selectList(replyQueryWrapper);
        return Result.success(replies);
    }

    @PostMapping("/replyAdd")
    @Transactional
    public Result addReply(@RequestBody Reply reply){
        TopicForum topicForum = topicForumMapper.selectById(reply.getForumId());
        if(topicForum!=null) {
            reply.setCreateTime(LocalDateTime.now());
            reply.setCreateUser(UserInfoContext.getUserInfo().getNickName() != null ? UserInfoContext.getUserInfo().getNickName() : UserInfoContext.getUserInfo().getUserName());
            replyMapper.insert(reply);
            topicForum.setReplyCount(topicForum.getReplyCount() + 1);
            topicForum.setLastReply(LocalDateTime.now());
            topicForumMapper.updateById(topicForum);
        }
        return Result.success();
    }

    @GetMapping("/hotForum")
    public Result getHotForum(){
        QueryWrapper<TopicForum> topicForumQueryWrapper = new QueryWrapper<>();
        topicForumQueryWrapper.gt("view_count",50);
        topicForumQueryWrapper.orderByDesc("view_count");
        List<TopicForum> topicForums = topicForumMapper.selectList(topicForumQueryWrapper);
        return Result.success(topicForums);
    }
}
